package com.wobito.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wobito.common.constants.BaseResponse;
import com.wobito.common.constants.TableDataInfo;
import com.wobito.common.utils.DateUtils;
import com.wobito.common.utils.ResultUtils;
import com.wobito.common.utils.StringUtils;
import com.wobito.pojo.bean.OperateLog;
import com.wobito.pojo.dto.log.BorrowLogDTO;
import com.wobito.pojo.dto.log.DeviceLogDTO;
import com.wobito.pojo.vo.log.BorrowReturnLogVO;
import com.wobito.pojo.vo.log.DeviceLogVO;
import com.wobito.pojo.vo.log.OperateLogVO;
import com.wobito.service.IBorrowReturnLogService;
import com.wobito.service.IDeviceLogService;
import com.wobito.service.IOperateLogService;
import com.wobito.system.aspect.OperLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author Wangbaba
 * @Date 2024/8/29 17:33
 * @Version 1.0
 */
@RestController
@RequestMapping("/manage/log")
@Api(tags = "日志记录")
public class LogController {

    @Autowired
    private IBorrowReturnLogService borrowReturnLogService;
    @Autowired
    private IDeviceLogService deviceLogService;
    @Autowired
    private IOperateLogService operateLogService;

    @GetMapping("/borrowReturnLog")
    @ApiOperation(value = "根据条件查询借阅归还日志")
    @OperLog(thing = "根据条件查询借阅归还日志")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NOMAL_ADMIN')")
    public BaseResponse getBorrowReturnLogList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                               @RequestParam(value = "queryName", required = false) String queryName,
                                               @RequestParam(value = "type", required = false) Integer type,
                                               @RequestParam(value = "startTime", required = false) String startTime,
                                               @RequestParam(value = "endTime", required = false) String endTime) {
        TableDataInfo dataInfo = new TableDataInfo();
        PageHelper.startPage(pageNum, pageSize);
        List<BorrowLogDTO> borrowLogDTOS = borrowReturnLogService.selectBorrowReturnLogList(new BorrowReturnLogVO(queryName, type, startTime, endTime));
        dataInfo.setRows(borrowLogDTOS);
        dataInfo.setTotal(new PageInfo(borrowLogDTOS).getTotal());
        return ResultUtils.success(dataInfo);
    }

    @GetMapping("/deviceLog")
    @ApiOperation(value = "查询设备操作日志")
    @OperLog(thing = "查询设备操作日志")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NOMAL_ADMIN')")
    public BaseResponse getDeviceLogList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "tableInfo", required = false) String tableInfo,
                                         @RequestParam(value = "latId", required = false) Long latId,
                                         @RequestParam(value = "queryName", required = false) String queryName,
                                         @RequestParam(value = "operTime", required = false) String operTime) {
        tableInfo = StringUtils.isNotEmpty(tableInfo) ? tableInfo : DateUtils.getYearAndMonth();
        TableDataInfo dataInfo = new TableDataInfo();
        PageHelper.startPage(pageNum, pageSize);
        List<DeviceLogDTO> deviceLogs = deviceLogService.selectDeviceList(new DeviceLogVO(tableInfo,latId,queryName, operTime));
        dataInfo.setRows(deviceLogs);
        dataInfo.setTotal(new PageInfo(deviceLogs).getTotal());
        return ResultUtils.success(dataInfo);
    }

    @GetMapping("/operateLog")
    @ApiOperation(value = "查询系统操作日志")
    @OperLog(thing = "查询系统操作日志")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NOMAL_ADMIN')")
    public BaseResponse getOperateLog(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(value = "tableInfo", required = false) String tableInfo,
                                      @RequestParam(value = "queryName", required = false) String queryName,
                                      @RequestParam(value = "time", required = false) String time) {
        tableInfo = StringUtils.isNotEmpty(tableInfo) ? tableInfo : DateUtils.getYearAndMonth();
        TableDataInfo dataInfo = new TableDataInfo();
        PageHelper.startPage(pageNum, pageSize);
        List<OperateLog> operateLogs = operateLogService.selectOperateLogList(new OperateLogVO(tableInfo,queryName, time));
        dataInfo.setRows(operateLogs);
        dataInfo.setTotal(new PageInfo(operateLogs).getTotal());
        return ResultUtils.success(dataInfo);
    }

}
